﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using WucCloud.Utility;

namespace WucCloud.WebEntry.Controllers
{
    /// <summary>
    /// 公共控制器 拓展类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 复写父类的该方法。执行控制器中的方法之前执行该方法。从而实现过滤的功能。
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            //调用父类该方法
            base.OnActionExecuting(context);
            //1.验证是否登录
            //2.验证菜单权限
            //3.验证按钮权限
            //在action执行之前
        }

        /// <summary>
        /// 复写父类的该方法。执行控制器中的方法之后执行该方法。从而实现过滤的功能。
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            //调用父类该方法
            base.OnActionExecuted(context);
        }

        protected virtual ActionResult Success(string message)
        {
            return Content(new UnifyResult 
            { 
                StatusCode = ResultCode.Success,
                Message = message 
            }.ToJson());
        }
        protected virtual ActionResult Success<T>(string message, T data)
        {
            return Content(new UnifyResult<T> 
            { 
                StatusCode = ResultCode.Success,
                Message = message, 
                Data = data 
            }.ToJson());
        }
        protected virtual ActionResult Success<T>(int total, T data)
        {
            return Content(new UnifyResult<T> 
            {
                StatusCode = ResultCode.Success,
                Message = "", 
                TotalCount = total,
                Data = data 
            }.ToJson());
        }
        protected virtual ActionResult Error(string message)
        {
            return Content(new UnifyResult 
            { 
                StatusCode = ResultCode.Error,
                Message = message 
            }.ToJson());
        }
        protected virtual ActionResult DTree(object data)
        {
            return Content(new DTreeResult 
            {
                status = new StatusInfo 
                { 
                    code = 200, 
                    message = "操作成功" 
                }, data = data 
            }.ToJson());
        }
        
        /// <summary>
        /// 当前登录用户信息
        /// </summary>
        public virtual OperatorUser CurrentUser
        {
            get => GlobalContext.CurrentUser();
        }

        #region 基础页面
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增/编辑页
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 详情页
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult Details()
        {
            return View();
        }
        #endregion

        #region 异常页面
        /// <summary>
        /// 异常页
        /// </summary>
        /// <returns></returns>
        public IActionResult ErrorException(string message)
        {
            return View(message);
        }

        /// <summary>
        /// 403页
        /// </summary>
        /// <returns></returns>
        public IActionResult Error403()
        {
            return View();
        }

        /// <summary>
        /// 404页
        /// </summary>
        /// <returns></returns>
        public IActionResult Error404()
        {
            return View();
        }

        /// <summary>
        /// 500页
        /// </summary>
        /// <returns></returns>
        public IActionResult Error500()
        {
            return View();
        }
        #endregion
    }
}
